package com.itheima.service.db.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.*;
import com.itheima.mapper.*;
import com.itheima.service.db.IOrderService;
import com.itheima.vo.*;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author fukeke
 * @since 2022-11-11
 */
@DubboService
public class OrderServiceImpl implements IOrderService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OmsOrderOperateHistoryMapper omsOrderOperateHistoryMapper;
    @Autowired
    OmsOrderItemMapper omsOrderItemMapper;
    @Autowired
    OmsOrderReturnApplyMapper omsOrderReturnApplyMapper;
    @Autowired
    OmsCompanyAddressMapper omsCompanyAddressMapper;
    @Autowired
    OmsOrderReturnReasonMapper omsOrderReturnReasonMapper;
    @Autowired
    OmsOrderSettingMapper omsOrderSettingMapper;

    @Override
    public Boolean orderDelete(Integer[] ids) {
        LambdaQueryWrapper<OmsOrder> qw=new LambdaQueryWrapper<>();
        qw.in(OmsOrder::getId,ids)
            .eq(OmsOrder::getStatus,4);
        int count = orderMapper.delete(qw);
//        orderMapper.deleteBatchIds(Arrays.asList(ids));
        if (count>0){
            return true;
        }else {
            return false;
        }

    }

    @Override
    public PageBeanVo findOrderList(String createTime, String orderSn, Integer orderType, Integer pageNum, Integer pageSize, String receiverKeyword, Integer sourceType, Integer status) {
        String phone=null;
        String name=null;
        String orderType1 = null;
        String sourceType1= null;
        String status1= null;
        String createTime1=null;
        if (StringUtils.isNotBlank(receiverKeyword)){
            if (receiverKeyword.length()>10){
                phone=receiverKeyword;
            }else {
                name=receiverKeyword;
            }
        }
        if (orderType!=null){
            orderType1=orderType.toString();
        }
        if (sourceType!=null){
            sourceType1=sourceType.toString();
        }
        if (status!=null){
            status1=status.toString();
        }
        if (createTime!=null){
            createTime1=createTime;
        }

        Page<OmsOrder> page=new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<OmsOrder> qw=new LambdaQueryWrapper<>();
        qw.eq(StringUtils.isNotBlank(createTime1),OmsOrder::getCreateTime,createTime1);
        qw.eq(StringUtils.isNotBlank(orderSn),OmsOrder::getOrderSn,orderSn);
        qw.eq(StringUtils.isNotBlank(orderType1),OmsOrder::getOrderType,orderType);
        qw.like(StringUtils.isNotBlank(phone),OmsOrder::getReceiverPhone,phone);
        qw.like(StringUtils.isNotBlank(name),OmsOrder::getReceiverName,name);
        qw.eq(StringUtils.isNotBlank(sourceType1),OmsOrder::getSourceType,sourceType);
        qw.eq(StringUtils.isNotBlank(status1),OmsOrder::getStatus,status);

        qw.orderByDesc(StringUtils.isNotBlank(createTime1),OmsOrder::getCreateTime);
        orderMapper.selectPage(page, qw);
        PageBeanVo pageBeanVo=new PageBeanVo(pageNum,pageSize,page.getTotal(),page.getRecords());
        return pageBeanVo;
    }

    @Override
    public Boolean orderClose(Integer[] ids) {
        LambdaQueryWrapper<OmsOrder> qw =new LambdaQueryWrapper<>();
        qw.eq(OmsOrder::getId,0)
        .or().eq(OmsOrder::getId,1)
        .or().eq(OmsOrder::getId,2)
        .or().eq(OmsOrder::getId,3);
        OmsOrder omsOrder=new OmsOrder();
        omsOrder.setStatus(4);
        int count = orderMapper.update(omsOrder, qw);
        if (count>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Boolean orderSend(List<OmsOrderVo> deliveryParamList) {
        List<Integer> ids=new ArrayList<>();
        for (OmsOrderVo omsOrderVo : deliveryParamList) {
            ids.add(omsOrderVo.getOrderId());
        }

        LambdaQueryWrapper<OmsOrder> qw=new LambdaQueryWrapper<>();
        qw.in(OmsOrder::getId,ids)
            .eq(OmsOrder::getStatus,1);
        OmsOrder omsOrder=new OmsOrder();
        omsOrder.setStatus(2);
        int count = orderMapper.update(omsOrder, qw);
        if (count>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public OmsOrderVo orderFindById(Integer id) {
        OmsOrder omsOrder =orderMapper.selectById(id);
        OmsOrderVo omsOrderVo=new OmsOrderVo();
        BeanUtil.copyProperties(omsOrder,omsOrderVo);

        LambdaQueryWrapper<OmsOrderOperateHistory> qw=new LambdaQueryWrapper<>();
        qw.eq(OmsOrderOperateHistory::getOrderId,id);
        List<OmsOrderOperateHistory> omsOrderOperateHistories = omsOrderOperateHistoryMapper.selectList(qw);


        LambdaQueryWrapper<OmsOrderItem> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(OmsOrderItem::getOrderId,id);
        List<OmsOrderItem> omsOrderItems = omsOrderItemMapper.selectList(queryWrapper);


        omsOrderVo.setHistoryList(omsOrderOperateHistories);
        omsOrderVo.setOrderItemList(omsOrderItems);
        return omsOrderVo;
    }

    @Override
    public Boolean orderNote(Integer id, String note, Integer status) {
        LambdaQueryWrapper<OmsOrder> qw=new LambdaQueryWrapper<>();
        qw.eq(OmsOrder::getId,id).eq(OmsOrder::getStatus,status);
        OmsOrder omsOrder=new OmsOrder();
        omsOrder.setNote(note);
        orderMapper.update(omsOrder,qw);
        return true;
    }

    @Override
    public void orderUpdatePeopel(OmsReceiverInfoParam receiverInfoParam) {
        Integer id = receiverInfoParam.getOrderId();
        LambdaQueryWrapper<OmsOrder> qw=new LambdaQueryWrapper<>();
        qw.eq(OmsOrder::getId,id);
        OmsOrder omsOrder=new OmsOrder();
        BeanUtil.copyProperties(receiverInfoParam,omsOrder);
        orderMapper.update(omsOrder,qw);
    }

    @Override
    public PageBeanVo returnApplyByPage(String createTime, String handleMan, String handleTime, Integer id, Integer pageNum, Integer pageSize, String receiverKeyword, Integer status) {
        String status1=null;
        String createTime1=null;
        String handleTime1=null;
        String idd=null;

        if (status!=null){
            status1=status.toString();
        }
        if (id!=null){
            idd=id.toString();
        }
        Page<OmsOrderReturnApply> page=new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<OmsOrderReturnApply> qw=new LambdaQueryWrapper<>();
        qw.eq(StringUtils.isNotBlank(idd),OmsOrderReturnApply::getId,id)
                .eq(StringUtils.isNotBlank(status1),OmsOrderReturnApply::getStatus,status1)
                .eq(StringUtils.isNotBlank(createTime1),OmsOrderReturnApply::getCreateTime,createTime1)
                .eq(StringUtils.isNotBlank(handleTime1),OmsOrderReturnApply::getHandleTime,handleTime1)
                .like(StringUtils.isNotBlank(handleMan),OmsOrderReturnApply::getHandleMan,handleMan);
        qw.orderByDesc(StringUtils.isNotBlank(createTime1),OmsOrderReturnApply::getCreateTime);
        omsOrderReturnApplyMapper.selectPage(page, qw);
        PageBeanVo pageBeanVo=new PageBeanVo(pageNum,pageSize, page.getTotal(), page.getRecords());
        return pageBeanVo;
    }

    @Override
    public Boolean returnApplyDelete(Integer[] ids) {
        LambdaQueryWrapper<OmsOrderReturnApply> qw=new LambdaQueryWrapper<>();
        qw.in(OmsOrderReturnApply::getId,ids)
            .eq(OmsOrderReturnApply::getStatus,2)
            .or().eq(OmsOrderReturnApply::getStatus,3);

        int delete = omsOrderReturnApplyMapper.delete(qw);
        if (delete>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public OmsOrderReturnApplyVo returnApplyById(Integer id) {
        OmsOrderReturnApply omsOrderReturnApply = omsOrderReturnApplyMapper.selectById(id);
        OmsOrderReturnApplyVo omsOrderReturnApplyVo=new OmsOrderReturnApplyVo();
        BeanUtils.copyProperties(omsOrderReturnApply,omsOrderReturnApplyVo);

        LambdaQueryWrapper<OmsCompanyAddress> qw=new LambdaQueryWrapper<>();
        qw.eq(OmsCompanyAddress::getId,omsOrderReturnApplyVo.getCompanyAddressId());
        List<OmsCompanyAddress> omsCompanyAddresses = omsCompanyAddressMapper.selectList(qw);
        omsOrderReturnApplyVo.setCompanyAddress(omsCompanyAddresses);
        return omsOrderReturnApplyVo;
    }

    @Override
    public void returnApplyUpdate(Integer id, OmsUpdateStatusParam statusParam) {
        LambdaQueryWrapper<OmsOrderReturnApply> qw=new LambdaQueryWrapper<>();
        qw.eq(OmsOrderReturnApply::getId,id);
        OmsOrderReturnApply omsOrderReturnApply=new OmsOrderReturnApplyVo();
        BeanUtil.copyProperties(statusParam,omsOrderReturnApply);
        omsOrderReturnApply.setHandleTime(LocalDateTime.now());
        omsOrderReturnApplyMapper.update(omsOrderReturnApply,qw);
    }

    @Override
    public void returnReasonSave(OmsOrderReturnReason returnReason) {
        returnReason.setCreateTime(LocalDateTime.now());
        omsOrderReturnReasonMapper.insert(returnReason);
    }

    @Override
    public PageBeanVo returnReasonByPage(Integer pageNum, Integer pageSize) {
        Page<OmsOrderReturnReason> page=new Page<>(pageNum,pageSize);
        omsOrderReturnReasonMapper.selectPage(page, null);
        PageBeanVo pageBeanVo=new PageBeanVo(pageNum,pageSize,page.getTotal(),page.getRecords());
        return pageBeanVo;
    }

    @Override
    public void returnReasonDeleteByIds(Integer[] ids) {
        omsOrderReturnReasonMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public void returnReasonUpdateStatus(Integer[] ids, Integer status) {
        LambdaQueryWrapper<OmsOrderReturnReason> qw=new LambdaQueryWrapper<>();
        qw.in(OmsOrderReturnReason::getId,ids);
        OmsOrderReturnReason omsOrderReturnReason=new OmsOrderReturnReason();
        omsOrderReturnReason.setStatus(status);
        omsOrderReturnReasonMapper.update(omsOrderReturnReason,qw);
    }

    @Override
    public void returnReasonUpdateReason(OmsOrderReturnReason returnReason) {
        Long id = returnReason.getId();
        LambdaQueryWrapper<OmsOrderReturnReason> qw=new LambdaQueryWrapper<>();
        qw.eq(OmsOrderReturnReason::getId,id);
        OmsOrderReturnReason omsOrderReturnReason=new OmsOrderReturnReason();
        BeanUtils.copyProperties(returnReason,omsOrderReturnReason);
        omsOrderReturnReasonMapper.update(omsOrderReturnReason,qw);
    }

    @Override
    public OmsOrderReturnReason returnReasonSelectById(Integer id) {
        LambdaQueryWrapper<OmsOrderReturnReason> qw=new LambdaQueryWrapper<>();
        qw.eq(OmsOrderReturnReason::getId,id);
        OmsOrderReturnReason omsOrderReturnReason = omsOrderReturnReasonMapper.selectOne(qw);
        return omsOrderReturnReason;
    }

    @Override
    public OmsOrderSetting omsOrderSettingfindById(Integer id) {
        return omsOrderSettingMapper.selectById(id);
    }

    @Override
    public void omsOrderSettingUpdate(Integer id, OmsOrderSetting orderSetting) {
        LambdaQueryWrapper<OmsOrderSetting> qw=new LambdaQueryWrapper<>();
        qw.eq(OmsOrderSetting::getId,id);
        OmsOrderSetting omsOrderSetting=new OmsOrderSetting();
        BeanUtils.copyProperties(orderSetting,omsOrderSetting);
        omsOrderSettingMapper.update(omsOrderSetting,qw);
    }

    @Override
    public void orderUpdateMoney(OmsMoneyInfoParam moneyInfoParam) {
        LambdaQueryWrapper<OmsOrder> qw=new LambdaQueryWrapper<>();
        qw.eq(OmsOrder::getId,moneyInfoParam.getOrderId());
        OmsOrder omsOrder=new OmsOrder();
        omsOrder.setFreightAmount(moneyInfoParam.getFreightAmount());
        omsOrder.setDiscountAmount(moneyInfoParam.getDiscountAmount());
        orderMapper.update(omsOrder,qw);
    }
}
